import 'package:flutter/material.dart';
import 'package:qgchat/common/global/global.dart';
import 'package:qgchat/http/apis/api.dart';
import 'package:qgchat/http/models/tokenuserInfoModel.dart';
import 'package:qgchat/states/states_model/index.dart';
import 'package:qgchat/widgets/tipsDialog.dart';

/// 视频通话逻辑类
class VideoCall {
  /// 检查用户状态并转到视频页面
  /// 回拨时 回拨id必传
  static void checkUserStatusAndGoVideoPage(
    BuildContext context,
    String targetToken, {
    int callbackId,
  }) async {
    UserState userState = (state(context, 'user') as UserState);
    bool isAnchor = userState.info.memberType == 1;

    if (userState.info.memberToken == targetToken) {
      showToast('你不能和自己视频的呀');
      return;
    }

    // ------- 主播发起的回拨 -------
    if (isAnchor) {
      Map result = await VideoApis.checkCallStatus(targetToken);
      if (result['status'] == 1) {
        // 获取用户信息，跳转视频页
        goPage(
          context,
          '/lodingCall',
          param: {'token': targetToken},
        );
        // TokenuserInfoModel userRes = await VideoApis.tokenuserInfo(targetToken);
        // goPage(context, '/live', param: {
        //   'isAnchor': true,
        //   'isReceive': false,
        //   'anchorId': userState.info.id,
        //   'userId': userRes.id,
        //   'targetName': userRes.memberNickname,
        //   'targetHead': userRes.memberHead,
        //   'targetToken': userRes.memberToken,
        // });
        // 改变回拨状态
        if (callbackId != null) {
          VideoApis.callback(callbackId);
        }
      } else {
        // 不可以视频的原因排查
        switch (result['data']['type']) {
          case 2: // 钱不够
            showToast('该用户没有足够的余额');
            break;
          case 3: // 状态不对(离线,在聊,勿扰)
            showToast('该用户暂时无法接通,请稍后再拨');
            break;
          default:
            showToast('拨打失败');
            break;
        }
      }
      return;
    }

    // ------- 用户发起的视频 -------
    // 检查是否可以视频
    Map result = await VideoApis.checkVideoStatus(targetToken);
    if (result['status'] == 1) {
      // 获取主播信息，跳转视频页
      goPage(
        context,
        '/lodingCall',
        param: {'token': targetToken},
      );
      // TokenuserInfoModel userRes = await VideoApis.tokenuserInfo(targetToken);
      // goPage(context, '/live', param: {
      //   'isAnchor': false,
      //   'isReceive': false,
      //   'userId': userState.info.id,
      //   'anchorId': userRes.id,
      //   'targetName': userRes.memberNickname,
      //   'targetHead': userRes.memberHead,
      //   'targetToken': userRes.memberToken,
      // });
    } else {
      // 不可以视频的原因排查
      switch (result['data']['type']) {
        case 1: // 已拉黑
          showToast('非常抱歉，小姐姐暂时与你无缘');
          break;
        case 2: // 钱不够
          TipsDialog.notGold(context, () => goPage(context, '/recharge'));
          break;
        case 3: // 状态不对(离线,在聊,勿扰)
          TipsDialog.makeAnchor(context, () async {
            Map makeRes = await VideoApis.appointment(targetToken);
            if (makeRes['status'] == 1) {
              showToast('预约成功');
            } else {
              showToast(makeRes['msg']);
            }
          });
          break;
        default:
          showToast('拨打失败');
          break;
      }
    }
  }

  /// 检查用户状态并转到私聊页面
  static Future checkUserStatusAndGoChatPage(
    BuildContext context,
    String token,
  ) async {
    TokenuserInfoModel userRes = await VideoApis.tokenuserInfo(token);

    UserState user = state(context, 'user');
    if (userRes.memberToken == user.info.memberToken) {
      showToast("请不要自言自语");
      return;
    }
    if (user.info.memberType == 0 && userRes.memberType == 0 && userRes.memberToken != Config.CUSTOMERSERVICESTAFF) {
      showToast("还是去私聊小姐姐吧");
      return;
    }

    if (userRes.memberType == 0) {
      return await goPage(
        context,
        '/chat',
        param: {
          'targetId': userRes.id,
          'targetName': userRes.memberNickname,
          'targetHead': userRes.memberHead,
          'targetToken': userRes.memberToken,
        },
      );
    }

    Map result = await CommonApis.checkTalkStatus(token);

    if (result['status'] == 1) {
      return await goPage(
        context,
        '/chat',
        param: {
          'targetId': userRes.id,
          'targetName': userRes.memberNickname,
          'targetHead': userRes.memberHead,
          'targetToken': userRes.memberToken,
        },
      );
    } else {
      if (result['data'] == null) {
        return await goPage(
          context,
          '/chat',
          param: {
            'targetId': userRes.id,
            'targetName': userRes.memberNickname,
            'targetHead': userRes.memberHead,
            'targetToken': userRes.memberToken,
          },
        );
      }

      switch (result['data']['type']) {
        case 1:
          showToast("非常抱歉，小姐姐暂时与你无缘");
          break;
        case 2:
          TipsDialog.notGold(context, () => goPage(context, '/recharge'));
          break;
      }
    }
  }
}
